/*
 * memory.c
 */

#include "memory.h"


/*
 * Initializes a new memory bank.
 */
Memory *new_memory(void)
{
	Memory *mem_ptr = malloc(sizeof(Memory));
	return mem_ptr;
}

/*
 * Sets the address at which the next read or write will occur.
 * memory: A pointer to the memory to modify.
 * value: The new value to set.
 */
void memory_set_memory_address_register(Memory *memory, Register value)
{
	memory->mar = value;
}

/*
 * Sets the data to be written to memory.
 * memory: A pointer to the memory to modify.
 * value: The new value to set.
 */
void memory_set_memory_data_register(Memory *memory, Register value)
{
	memory->mdr = value;
}

/*
 * Gets the data most recently read from memory.
 * memory: A pointer to the memory to query.
 */
Register memory_get_memory_data_register(Memory *memory)
{
	return memory->mdr;
}

/*
 * Gets the value at the memory address stored in the MAR and puts it in the MDR
 * This operation is undefined if the address register hasn't been set.
 * memory: A pointer to the memory to query.
 */
void memory_perform_read(Memory *memory)
{
	memory->mdr = memory->mem_block[memory->mar];
}

/*
 * Sets the value at the memory address stored in MAR to the value in MDR.
 * This operation is undefined if the address register or the
 * data register hasn't been set.
 */
void memory_perform_write(Memory *memory)
{
	memory->mem_block[memory->mar] = memory->mdr;
}

/*
 * Frees the memory object from system memory.
 * memory: The memory to delete.
 */
void delete_memory(Memory *memory)
{
	free(memory);
}
